#!/usr/bin/perl -w
use lib qw (/home/yenyu/linux/perl);
use lib qw (/home/yenyu/linux/perl/blib/lib);
use lib qw (/home/yenyu/linux/perl/blib/arch);
#INPUT format:
# url_id lang
#use BerkeleyDB ;

if(-s $ARGV[0]) {
    open(fd_ss,$ARGV[0]) || die "can't open input file for read:$!";
}else{
    print "Usage: query_to_id.keywords_unsorted\n";
    exit;
}

%hash_check=();
$line = 0;
$count = 0;
$count_hit = 0;

while(<fd_ss>){
        chomp;
        $line++;
 	        if( $line%100000 == 0 )
        {
                print $line/1000, "k\n";
        }

        if( $line< 15000000 )
        {
                @aa = split( /\t/, $_);
                @a = split( /\s/, $aa[0] );
                $count_internal = 0 ;
                foreach $a (@a)
                {
                        $count_internal++;
                        if( $count_internal < @a )
                        {
                                $b = $a." ".$a[$count_internal] ;
                                if( !defined($hash_check{$b} ))
                                {
                                        $hash_check{$b} = 1;
                                }
                        }
                }
        }
        else
        {
				if ( @a == 3 )
				{
					$count++;
					@aa = split(/\t/, $_);
					@a = split(/\s/, $aa[0] );
					$count_pair = 0;
					$i = 0 ;
					while( $i < 2)
					{
						$b = $a[$i]." ".$a[$i+1] ;
						if( defined($hash_check{$b}) )
						{
								$count_pair++;
						}
					}
					#if($count_pair == 0)
					#if($count_pair == 2)
					if( $count_pair == 1)
					{
						$count_hit++;
					}

				}
		}
}

print "total terms in last queries:", $count, " hit terms:", $count_hit, "\n";
print "perc of hit: ", $count_hit/$count, "\n";
close(fd_ss);
